
#include "precompiled.h"
#include "tcpserver.h"
#include "tasks/servertask.h"
#include "engine.h"
#include "precompiled.h"
#include "tasks.h"
#include "configstack.h"
#include "helper.h"


namespace Optra{ namespace Engine{ namespace Server { namespace TcpServer {


TcpServerPrivate::TcpServerPrivate():QTcpServer(), disabled(false), port(51999)
{
}

TcpServerPrivate::~TcpServerPrivate()
{
}

void TcpServerPrivate::start()
{
	//if (!disabled){
	if (listen(QHostAddress::Any, port)){
			logger()->info("TcpServer Start on host:%1 ,port:%2", serverAddress().toString(), serverPort());
		}else{
			logger()->error("TcpServer unable to start:%1", errorString());
		}
	/*}else{
		logger()->info("Tcp Server is disabled. you can enable by set section \"server {enabled:true}\" in config file");
	}*/
}




void TcpServerPrivate::incomingConnection(int socketDescriptor)
{
	ServerTask* task = new ServerTask(socketDescriptor);
	::Engine::instance()->tasks()->start(task, tr("TCP Server connection")); 
}
	
	TcpServer::TcpServer(const QString& configKey, QObject *parent) :
	ConnectionProvider(), O_PRIVATE_CREATE(TcpServer)
	{
		logger()->info("Script server initialize");

		O_D(TcpServer);
		//d->disabled =!          conf(configKey, "enabled", true).toBool();
		//d->port =               conf(configKey, "port", 51999).toInt();

		logger()->info("Config:");
		//logger()->info("disabled: %1", d->disabled);
		//logger()->info("port: %1", d->port);
	}

	TcpServer::~TcpServer()
	{ 
		O_PRIVATE_DELETE;
	}

	void TcpServer::Start()
	{
		logger()->info("Start");
		O_D(TcpServer);
		d->start();
	}

}}}}